<template>
  <div>
    <h1>{{id ? '修改' : '新建'}}英雄{{model.name ? '--' + model.name : ''}}</h1>
    <el-form label-width="100px" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="代号">
            <el-input type="number" v-model="model.ename"></el-input>
          </el-form-item>
          <el-form-item label="职业">
            <el-select v-model="model.heroType" multiple>
              <el-option v-for="item in heroType" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeaders()" :show-file-list="false" :on-success="avatarUpload">
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate v-model="model.scores.difficult" :max="10" show-score></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate v-model="model.scores.skill" :max="10" show-score></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate v-model="model.scores.attack" :max="10" show-score></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate v-model="model.scores.survive" :max="10" show-score></el-rate>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能">
          <el-row :gutter="20" type="flex" style="flex-wrap:wrap;">
            <el-col :span="12" v-for="(item, index) in model.skills" :key="index">
              <div class="colStyle">
                <el-form-item label="图标">
                  <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeaders()" :show-file-list="false" :on-success="res => $set(item, 'icon', res.url)">
                    <img v-if="item.icon" :src="item.icon" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
                <el-form-item label="名称">
                  <el-input v-model="item.name"></el-input>
                </el-form-item>
                <el-form-item label="冷却值">
                  <el-input v-model="item.cd"></el-input>
                </el-form-item>
                <el-form-item label="消耗">
                  <el-input v-model="item.blue"></el-input>
                </el-form-item>
                <el-form-item label="技能描述">
                  <el-input type="textarea" v-model="item.description"></el-input>
                </el-form-item>
                <el-form-item label="技能提示">
                  <el-input type="textarea" v-model="item.tip"></el-input>
                </el-form-item>
                <el-button class="middle" type="danger" icon="el-icon-delete" circle @click="model.skills.splice(index, 1)"></el-button>
              </div>
            </el-col>
          </el-row>
          <el-button type="primary" round class="middle" @click="model.skills.push({})"><i class="el-icon-plus"></i> 添加技能</el-button>
        </el-tab-pane>
        <el-tab-pane label="出装技巧">
          <el-form-item label="技能加点">
            <el-select v-model="model.point.skill" multiple placeholder="主升/副升">
              <el-option v-for="(item, i) in model.skills" :key="i" :label="item.name" :value="i"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="召唤师技能">
            <el-select v-model="model.point.summoner" multiple>
              <el-option v-for="(item, i) in summoners" :key="i" :label="item.summoner_name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1.items" multiple filterable>
              <el-option v-for="item in items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
            <el-input type="textarea" v-model="model.items1.tip" style="margin-top:1rem;" placeholder="tip"></el-input>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2.items" multiple filterable>
              <el-option v-for="item in items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
            <el-input type="textarea" v-model="model.items2.tip" style="margin-top:1rem;" placeholder="tip"></el-input>
          </el-form-item>
          <el-form-item label="铭文选择" class="ming_select">
            <el-select v-for="(cat, i) in mings" :key="i" v-model="model.mings[i]" :placeholder="cat.type" filterable>
              <el-option v-for="(ming, index) in cat.items" :key="index" :label="ming.ming_name" :value="ming._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="皮肤">
          <el-row :gutter="110" v-if="model.skinName">
            <el-col :span="10">
              <el-form-item v-for="(skin, index) in model.skinName" :key="index" :label="'皮肤'+(index+1)">
                <el-input v-model="model.skinName[index]"></el-input>
                <div style="width:90px;position:absolute;right:-100px;top:0;">
                  <el-button v-if="index===model.skinName.length-1" type="info" circle @click="model.skinName.push('')"><i class="el-icon-plus"></i></el-button>
                  <el-button v-if="model.skinName[index]===''" type="info" circle @click="model.skinName.pop()"><i class="el-icon-minus"></i></el-button>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-carousel v-if="this.model.ename" direction="vertical" indicator-position="none">
                <el-carousel-item v-for="(item, index) in model.skinName" :key="index">
                  <img style="width:100%;" :src="`https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/${model.ename}/${model.ename}-bigskin-${index+1}.jpg`" alt="">
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="英雄关系">
          <el-col class="colStyle" v-for="(item, index) in model.related" :key="index">
            <div style="width:1px;color:#409eff;float:left;margin-top:1rem;">{{index | ctitle}}</div>
            <el-col :span="11" v-for="(hero, i) in item" :key="i">
              <el-form-item label="英雄">
                <el-select v-model="hero.name" filterable>
                  <el-option v-for="h in heroes" :key="h._id" :label="h.name" :value="h._id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="hero.description" placeholder="tip"></el-input>
              </el-form-item>
            </el-col>
          </el-col>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem;">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  filters: {
    ctitle (val) {
      switch (val) {
        case 'partners':
          return '最强搭档'
        case 'weak':
          return '被谁克制'
        default:
          return '克制谁'
      }
    }
  },
  props: {
    id: String
  },
  data () {
    return {
      model: {
        title: '',
        scores: {
          difficult: 0,
          skill: 0,
          attack: 0,
          survive: 0
        },
        skinName: [],
        point: {},
        items1: {},
        items2: {},
        skills: [],
        mings: [],
        related: {
          partners: [],
          weak: [],
          strong: []
        }
      },
      heroType: [],
      items: [],
      heroes: [],
      mings: [],
      summoners: []
    }
  },
  methods: {
    spaceMore (arr, num, obj) {
      if (arr.length < num) {
        arr.push(obj)
        this.spaceMore(arr, num, obj)
      } else {
        return false
      }
    },
    async save () {
      if (this.model.name.trim() === '') {
        this.$message.error('请输入英雄名称')
        return
      }
      if (this.id) {
        await this.$http.put(`/rest/heros/${this.id}`, this.model)
        this.$message.success('修改成功')
      } else {
        await this.$http.post('/rest/heros', this.model)
        this.$message.success('保存成功')
      }
      this.$router.push('/heros/list')
    },
    async getName () {
      const { data: res } = await this.$http.get(`/rest/heros/${this.id}`)
      this.model = Object.assign({}, this.model, res)
    },
    async getHeroType () {
      const { data: res } = await this.$http.get('/rest/categories')
      this.heroType = res.filter(item => item.parent && item.parent.name === '英雄列表')
    },
    async getItems () {
      const { data: res } = await this.$http.get('/rest/items')
      this.items = res
    },
    async getHeroes () {
      const { data: res } = await this.$http.get('/rest/heros')
      this.heroes = res
    },
    async getMings () {
      const { data: res } = await this.$http.get('/rest/mings')
      const garde5 = res.filter(ming => ming.ming_grade === 5)
      this.mings = [
        {
          type: 'red',
          items: garde5.filter(ming => ming.ming_type === 'red')
        },
        {
          type: 'green',
          items: garde5.filter(ming => ming.ming_type === 'green')
        },
        {
          type: 'blue',
          items: garde5.filter(ming => ming.ming_type === 'blue')
        }
      ]
    },
    async getSummoners () {
      const { data: res } = await this.$http.get('/rest/summoners')
      this.summoners = res
    },
    avatarUpload (res) {
      this.$set(this.model, 'avatar', res.url)
      // this.model.icon = res.url
    }
  },
  created () {
    this.id && this.getName()
    this.getHeroType()
    this.getItems()
    this.getHeroes()
    this.getMings()
    this.getSummoners()
    Object.keys(this.model.related).map(key => {
      this.spaceMore(this.model.related[key], 2, {})
    })
    this.spaceMore(this.model.skills, 1, {})
    this.spaceMore(this.model.skinName, 1, '')
    this.spaceMore(this.model.mings, 3, '')
  }
}
</script>

<style lang="less" scoped>
.middle {
  display: block;
  margin: 0 auto;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  width: 5rem;
  height: 5rem;
  display: block;
}
.el-rate {
  margin-top: 0.6rem;
}
.el-col {
    margin-bottom: 20px;
}
.colStyle {
  background:#f2f6fc;
  border-radius: 1rem;
  padding: 1rem;
}
.ming_select {
  .el-form-item__content {
    display: flex;
    .el-select {
      margin-right: 10px;
    }
  }
}
</style>
